import store from '@/store'


export const authNav = (title = '您还未登录，请登录后执行操作！', duration = 2000) => {
  global.ctrl && clearTimeout(global.ctrl)
  uni.showToast({
    title,
    icon: 'none',
    mask: true,
    duration
  })
  global.ctrl = setTimeout(() => {
    uni.navigateTo({
      url: '/subcom-my/Auth/Auth'
    })
  }, 2000)
}

export const checkToken = (url) => {
  const token = uni.getStorageSync('token')
  if (!store.state.auth.token || !token) {
    uni.showModal({
      title: '您还未登录',
      content: '登录后才能操作，确定登录吗？',
      success: function (res) {
        if (res.confirm) {
          uni.navigateTo({
            url: '/subcom-my/Auth/Auth'
          })
        } else if (res.cancel) {
          if (url !== 'undefined') {
            uni.switchTab({
              url
            })
          }
        }
      }
    })
    return false
  } else {
    return true
  }
}
export const checkAuth = () => {
  let flag = false
  const token = uni.getStorageSync('token')
  if (store.state.auth.token || token) {
    flag = true
  }

  return flag
}

export const gotoGuard = async (path) => {
  const flag = checkAuth()
  if (flag) {
    uni.navigateTo({
      url: path
    })
  } else {
    await authNav()
  }
}